package ui.fragment.homepagefragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import adapter.WorkAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.WorkBean;
import student.andlei.com.student.R;
import ui.activity.WorkActivity;
import utils.GsonUtils;

/**
 * 作业
 */
public class WorkFragment extends BaseNetFragment{
    private RecyclerView recyclerView;
    private WorkAdapter adapter;
    private List<WorkBean> list = new ArrayList<>();
    public static Fragment getInstance() {
        return new WorkFragment();
    }
    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            list = GsonUtils.getInstance(context).parseNoHeaderJArray(baseBean,WorkBean.class);
            adapter.setNewData(list);
        }

    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work;
    }

    @Override
    protected void findViews() {
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        //创建布局管理
        LinearLayoutManager layoutManager = new GridLayoutManager(this.context,4);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new WorkAdapter(getActivity(),R.layout.item_work,list);
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEmptyView(R.layout.empty_view);
        ((TextView)(adapter.getEmptyView().findViewById(R.id.tv_empty))).setText("你还没有作业哦");
        //
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("subject_id",list.get(position).subject_id+"");
                bundle.putString("subject_name",list.get(position).subject_name);
                jumpTo(WorkActivity.class,bundle);
            }
        });
    }

    @Override
    protected void formatData() {
        loadData();
//        for (int i=1;i<=10;i++){
//            WorkBean bean = new WorkBean();
//            bean.setFinish("已完成："+i+"份");
//            bean.setImgurl("88");
//            bean.setSubject("语文");
//            list.add(bean);
//        }
    }
    private void loadData(){
        post("Task/subjectList",BaseBean.class,false);
    }
    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {

    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loadData();
        }
    }
}
